#include "mangamanagerxml.h"
#include "mangalist.h"

MangaManagerXml::MangaManagerXml()
{
    m_list = new MangaList();
    QFile f("mangalist.xml");
    m_valid = f.open(QIODevice::ReadOnly);
    if(m_valid)
    {
	QXmlStreamReader r(&f);
	m_list->deserialize(&r);
	f.close();
    }
    m_nextId = ((Resource*)m_list->at(0))->getId();
    for(int i=0;i<m_list->count();i++)
    {
	m_nextId = m_nextId < ((Resource*)m_list->at(i))->getId()?((Resource*)m_list->at(i))->getId():m_nextId;
    }
}
MangaManagerXml::~MangaManagerXml()
{
    QFile f("mangalist.xml");
    m_valid = f.open(QIODevice::WriteOnly);
    if(m_valid)
    {
	QXmlStreamWriter w(&f);
	m_list->serialize(&w);
	f.close();
    }
    delete m_list;
}
ResourceList * MangaManagerXml::getResources()
{
    return m_list;
}
bool MangaManagerXml::addResource(Resource * r)
{
    m_list->append(r);
    return true;
}
bool MangaManagerXml::updateResource(Resource * r)
{
    int count = m_list->count();
    for(int i=0;i<count;i++)
    {
	Manga* mn = (Manga*)m_list->at(i);
	if(r->getId() == mn->getId())
	{
	    mn->setNom(((Manga*)r)->nom());
	    mn->setAdresse(((Manga*)r)->adresse());
	}
    }
    return true;
}
bool MangaManagerXml::deleteResource(Resource * r)
{
    int count = m_list->count();
    for(int i=0;i<count;i++)
    {
	Manga* mn = (Manga*)m_list->at(i);
	if(mn->getId() == r->getId())
	{
	    m_list->removeAt(i);
	}
    }
    return true;
}
QString MangaManagerXml::getLastError()
{
    return "";
}
